package com.bjsxt.shop.serviceImpl.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.util.PageUtil;
import com.bjsxt.shop.serviceApi.pojo.AAdmins;
import com.bjsxt.shop.serviceApi.service.IUserService;
import com.bjsxt.shop.serviceImpl.dao.IAAdminsDao;


@Service("userService")
public class UserServiceImpl implements IUserService
{
	private List<AAdmins> adminsList = new ArrayList<AAdmins>();
	
	@Resource
	private IAAdminsDao adminsDao;
	
	@Override
	public JSONObject saveOneAAdmins(AAdmins admin)
	{
		/*接收插入一条管理员影响的操作数量*/
		int res = this.adminsDao.save(admin);
		JSONObject resultJSON = new JSONObject();
		if(res > 0)
		{
			/*插入成功，拼装json*/
			resultJSON.put("code", "0");
			resultJSON.put("info", "插入成功");
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admin.getId());
			dataJSON.put("effect", res);
			resultJSON.put("data", dataJSON);
			return resultJSON;
		}
		/*插入不成功*/
		resultJSON.put("code", "1");
		resultJSON.put("info", "插入失败");
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAAdmins(AAdmins admin)
	{
		/*接收更新一条管理员影响的操作数量*/
		int res = this.adminsDao.update(admin);
		JSONObject resultJSON = new JSONObject();
		if(res > 0)
		{
			/*更新成功，拼装json*/
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admin.getId());
			dataJSON.put("effect", res);
			resultJSON.put("data", dataJSON);
			return resultJSON;
		}
		/*更新不成功*/
		resultJSON.put("code", "1");
		resultJSON.put("info", "更新失败");
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneAAdmins(AAdmins admin)
	{
		/*接收删除一条管理员影响的操作数量*/
		int res = this.adminsDao.delete(admin);
		JSONObject resultJSON = new JSONObject();
		if(res > 0)
		{
			/*删除成功，拼装json*/
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admin.getId());
			dataJSON.put("effect", res);
			resultJSON.put("data", dataJSON);
			return resultJSON;
		}
		/*删除不成功*/
		resultJSON.put("code", "1");
		resultJSON.put("info", "删除失败");
		return resultJSON;
	}

	@Override
	public AAdmins findOneAAdmins(Map<String, Object> condMap)
	{
		AAdmins admin = this.adminsDao.findOne(condMap);
		return admin;
	}

	@Override
	public List<AAdmins> findCondAAdminsList(Map<String, Object> condMap,PageUtil page)
	{
		/*实现模糊查询,处理模糊查询参数*/
		if(condMap.get("keyWord") != null && !"".equalsIgnoreCase(condMap.get("keyWord")+""))
		{
			condMap.put("keyWord", "%"+condMap.get("keyWord")+"%");
		}
		if(page != null)
		{
			/*表示分页，先查询总记录数 pageCount = true*/
			condMap.put("pageCount", "true");
			AAdmins admin = (AAdmins) this.adminsDao.findCondMap(condMap);
			page.setTotalRecord(admin.getId());
			condMap.put("pageCount", "false");
			adminsList = this.adminsDao.findCondMap(condMap);
			return adminsList;
		}
		adminsList = this.adminsDao.findCondMap(condMap);
		return adminsList;
	}

}
